﻿using ShanNiang.Mvvm.ViewModels;
using ShanNiang.Packing.UserControls;
using ShanNiang.Service;
using ShanNiang.Service.Attributes;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Versioning;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;


namespace ShanNiang.Packing
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    //[DILifetime(LifetimeType.Singleton)]
    [SupportedOSPlatform("windows")]
    public partial class MainWindow : Window
    {

        private readonly PackingConfig _packingConfig;

        public MainWindow(MainWindowViewModel mainFormViewModel,
            BindControl bindControl,
            LoginControl loginControl,
            LogoutControl logoutControl,
            PackBoxHistoryControl packingHistoryControl,
            PackBoxControl packingControl,
            PackPalletControl packPalletControl,
            PackingConfig packingConfig,
            PackPalletHistoryControl packingPalletHistoryControl)
        {
            _packingConfig = packingConfig;
            mainFormViewModel.ClearMenuViewModels();

            AddMenuCodeTo(mainFormViewModel, bindControl);
            AddMenuCodeTo(mainFormViewModel, loginControl);
            AddMenuCodeTo(mainFormViewModel, logoutControl);
            AddMenuCodeTo(mainFormViewModel, packingHistoryControl);
            AddMenuCodeTo(mainFormViewModel, packingControl);
            AddMenuCodeTo(mainFormViewModel, packPalletControl);
            AddMenuCodeTo(mainFormViewModel, packingPalletHistoryControl);
            InitializeComponent();

            DataContext = mainFormViewModel;

        }

        private static void AddMenuCodeTo(MainWindowViewModel mainFormViewModel, UserControl userControl)
        {
            var type = userControl.GetType();
            var attr = type.GetCustomAttribute<MenuControlAttribute>();
            if (attr != null)
            {
                var menuViewModel = new MenuViewModel(attr, userControl);
                mainFormViewModel.AddMenuViewModel(attr.MenuCode, menuViewModel);
            }
        }

        private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true; // 取消回车键的默认行为
        }



        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                this.DragMove();
            }
        }

    }
}